package old.day20190318.statements;

/**
 * @author DingZhenYun
 * @create 2019-03-18 11:17
 */

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 操作 JDBC 的工具类. 其中封装了一些工具方法 Version 1
 */
public class JDBCTools {
    public static void release(ResultSet rs,Statement statement, Connection connection){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void release(Statement statement, Connection connection){
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /*** 1. 获取连接的方法. 通过读取配置文件从数据库服务器获取一个连接.
	 **
	 */
    public static  Connection getConnection() throws Exception{
        Properties properties = new Properties();
        InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
                "jdbc.properties");
        properties.load(in);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        return DriverManager.getConnection(jdbcUrl, user, password);
    }

}
